package com.example.agriapp.view.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import com.example.agriapp.R;
import com.example.agriapp.model.bean.Article;

public class ArticleDetailActivity extends AppCompatActivity {

    private WebView webView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_detail);

        webView = findViewById(R.id.webview);
        progressBar = findViewById(R.id.progress_bar);
       findViewById(R.id.iv_back).setOnClickListener(view ->  finish());

        // 获取传递过来的文章数据
        Article article = (Article) getIntent().getSerializableExtra("article");
        
        setupWebView();
        displayArticle(article);
    }

    private void setupWebView() {
        WebSettings webSettings = webView.getSettings();
        
        // 基础设置
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        
        // 自适应屏幕
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        
        // 设置WebView客户端
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                progressBar.setVisibility(View.GONE);
            }
        });
    }

    private void displayArticle(Article article) {
        // 构建HTML内容
        String htmlContent = buildHtmlContent(article);
        
        // 加载HTML内容
        webView.loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null);
    }

    private String buildHtmlContent(Article article) {
        return "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>" + escapeHtml(article.getTitle()) + "</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: 'Helvetica Neue', Arial, sans-serif;\n" +
                "            line-height: 1.6;\n" +
                "            color: #333;\n" +
                "            padding: 16px;\n" +
                "            max-width: 800px;\n" +
                "            margin: 0 auto;\n" +
                "        }\n" +
                "        .article-title {\n" +
                "            font-size: 24px;\n" +
                "            font-weight: bold;\n" +
                "            margin-bottom: 16px;\n" +
                "            color: #222;\n" +
                "        }\n" +
                "        .article-meta {\n" +
                "            color: #999;\n" +
                "            font-size: 14px;\n" +
                "            margin-bottom: 20px;\n" +
                "        }\n" +
                "        .article-image {\n" +
                "            width: 100%;\n" +
                "            height: auto;\n" +
                "            margin-bottom: 20px;\n" +
                "            border-radius: 4px;\n" +
                "        }\n" +
                "        .article-content {\n" +
                "            font-size: 16px;\n" +
                "        }\n" +
                "        .article-content img {\n" +
                "            max-width: 100%;\n" +
                "            height: auto;\n" +
                "            margin: 10px 0;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h1 class=\"article-title\">" + escapeHtml(article.getTitle()) + "</h1>\n" +
                "    <div class=\"article-meta\">" + escapeHtml(article.getPublishTime()) + "</div>\n" +
                "    <img class=\"article-image\" src=\"" + article.getImageUrl() + "\" alt=\"文章封面图\">\n" +
                "    <div class=\"article-content\">" + article.getContent() + "</div>\n" +
                "</body>\n" +
                "</html>";
    }

    private String escapeHtml(String html) {
        if (html == null) {
            return "";
        }
        return html.replace("&", "&amp;")
                .replace("<", "&lt;")
                .replace(">", "&gt;")
                .replace("\"", "&quot;")
                .replace("'", "&#39;");
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}